$(function() {
	
				var $form_div = $("#dlPQJLMonth_form div");
				winResize($form_div);
				$(window).resize(function(){//当调整浏览器窗口的大小时，发生 resize 事件
					winResize($form_div);
				});
				dateFormatter();
				
				loadTable();
				
})


var $form = $('#dlPQJLMonth_form');

function  loadTable()
{	
	var columnsAll = new Array();
	columnsAll.push({field:'ids',title:'ids',align:'center',rowspan:2,hidden:true});
	columnsAll.push({field:'xzrs',title:'新增人数',align:'center',rowspan:2,
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'lcs',title:'疗程数',align:'center',rowspan:2,
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	
	columnsAll.push({field:'fwcs',title:'服务次数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'fwrs',title:'服务人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	columnsAll.push({field:'sjjzjs',title:'实际<br/>减重斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'xgkyjjs',title:'当年当月<br/>新增顾客<br/>应减斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'dndyxgksjjs',title:'当年新增<br/>顾客当月<br/>实减斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'ljjzl',title:'累计减重<br/>率',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){ return  value.toFixed(2)+"%";}
		}
	});

	columnsAll.push({field:'dbrs',title:'达标人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	columnsAll.push({field:'jzzxse',title:'减重销售',width:65,rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'cpxs',title:'产品销售',colspan:10,align:'center'});
	
	columnsAll.push({field:'pricetotal',title:'总销售',width:65,rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	
	var columnsAll1 = new Array();
	
	
	columnsAll1.push({field:'hf001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf003salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf004salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf005salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf011salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'jyg',title:'精油、膏',width:65,align:'center',
		formatter: function(value,row,index)
		{	
			var jyg = parseFloat(row.ss001salprice)+parseFloat(row.ss002salprice);
			if(jyg == ""){
				return "";
			}else{
				return  jyg.toFixed(1);
			}
			
		}
	});
	columnsAll1.push({field:'qt',title:'其他产品',width:60,align:'center',
		formatter: function(value,row,index)
		{	
			var qt = parseFloat(row.hf001salprice)+parseFloat(row.hf002salprice)
			+parseFloat(row.hf003salprice)
			+parseFloat(row.hf004salprice)
			+parseFloat(row.hf005salprice)
			+parseFloat(row.hf011salprice);
			if(qt == ""){
				return "";
			}else{
				return  qt.toFixed(1);
			}
			
			
		}
	});
	
	$('#dlPQJLMonth_datagrid').datagrid({
		cls: "theme-datagrid",
		singleSelect:true,
		frozenColumns:[[{field:'orgname',title:'片区经理',width:60,align:'center',
			formatter: function(value,row,index){
				var ids = row.organids;
			return '<a href="#" style="color: blue;" class="button-edit" onclick="lookInfo(\'' + ids + '\')">' + value + '</a>';
		}}]],
		fitColumns:false,
		rownumbers: true,
	    columns:[columnsAll,columnsAll1],
	    url: "dlPQJLMonthTotalStatistic/getMonthTotal",
	    queryParams: sy.serializeObject($form),
	   
	 	method: 'post',
	    onLoadSuccess:function(data){
	    	var n = data.total;
			 var rows = $('#dlPQJLMonth_datagrid').datagrid('getRows');
			if(rows.length > 0){
				var xgkyjjssum = compute_GXC('xgkyjjs');
				var xgksjjs = compute_GXC('dndyxgksjjs');
				var xzrs = compute_GXC('xzrs');
				var lcs = compute_GXC('lcs');
				var fwcs = compute_GXC('fwcs');
				var fwrs = compute_GXC('fwrs');
				var sjjzjs = compute_GXC('sjjzjs');
				var xgkyjjs = compute_GXC('xgkyjjs');
				var dndyxgksjjs = compute_GXC('dndyxgksjjs');
				var dbrs = compute_GXC('dbrs');
				var jzzxse = compute_GXC('jzzxse');
				var hf001salnum = compute_GXC('hf001salnum');
				var hf001salprice = compute_GXC('hf001salprice');
				var hf002salnum = compute_GXC('hf002salnum');
				var hf002salprice = compute_GXC('hf002salprice');
				var hf003salnum = compute_GXC('hf003salnum');
				var hf003salprice = compute_GXC('hf003salprice');
				var hf004salnum = compute_GXC('hf004salnum');
				var hf004salprice = compute_GXC('hf004salprice');
				var hf005salnum = compute_GXC('hf005salnum');
				var hf005salprice = compute_GXC('hf005salprice');
				var hf011salnum = compute_GXC('hf011salnum');
				var hf011salprice = compute_GXC('hf011salprice');
				
				var ss001salnum = compute_GXC('ss001salnum');
				var ss001salprice = compute_GXC('ss001salprice');
				var ss002salnum = compute_GXC('ss002salnum');
				var ss002salprice = compute_GXC('ss002salprice');
				
				
				var pricetotal = compute_GXC('pricetotal');
				var m = 0;
				if(xgkyjjssum!=0){
					m = parseFloat(xgksjjs)/parseFloat(xgkyjjssum) * 100;
					
				}
				$('#dlPQJLMonth_datagrid').datagrid('insertRow', {
					index:0,
					row:{orgname: '合计',
					xzrs:xzrs,
					lcs:lcs,
					fwcs:fwcs,
					fwrs:fwrs,
					sjjzjs:sjjzjs,
					xgkyjjs:xgkyjjs,
					dndyxgksjjs:dndyxgksjjs,
					ljjzl:m,
					dbrs:dndyxgksjjs,
					jzzxse:jzzxse,
					hf001salnum:hf001salnum,
					hf001salprice:hf001salprice,
					hf002salnum:hf002salnum,
					hf002salprice:hf002salprice,
					hf003salnum:hf003salnum,
					hf003salprice:hf003salprice,
					hf004salnum:hf004salnum,
					hf004salprice:hf004salprice,
					hf005salnum:hf005salnum,
					hf005salprice:hf005salprice,
					hf011salnum:hf011salnum,
					hf011salprice:hf011salprice,
					
					ss001salnum:ss001salnum,
					ss001salprice:ss001salprice,
					ss002salnum:ss002salnum,
					ss002salprice:ss002salprice,
					
					
					pricetotal:pricetotal
					}
	             });
				
				$('#dlPQJLMonth_datagrid').datagrid('insertRow', {
					index:1,
					row:{orgname: '店均',
					xzrs:(xzrs/n).toFixed(1),
					lcs:(lcs/n).toFixed(1),
					fwcs:(fwcs/n).toFixed(1),
					fwrs:(fwrs/n).toFixed(1),
					sjjzjs:(sjjzjs/n).toFixed(1),
					xgkyjjs:(xgkyjjs/n).toFixed(1),
					dndyxgksjjs:(dndyxgksjjs/n).toFixed(1),
					ljjzl:m,
					dbrs:(dndyxgksjjs/n).toFixed(1),
					jzzxse:(jzzxse/n).toFixed(1),
					hf001salnum:(hf001salnum/n).toFixed(1),
					hf001salprice:(hf001salprice/n).toFixed(1),
					hf002salnum:(hf002salnum/n).toFixed(1),
					hf002salprice:(hf002salprice/n).toFixed(1),
					hf003salnum:(hf003salnum/n).toFixed(1),
					hf003salprice:(hf003salprice/n).toFixed(1),
					hf004salnum:(hf004salnum/n).toFixed(1),
					hf004salprice:(hf004salprice/n).toFixed(1),
					hf005salnum:(hf005salnum/n).toFixed(1),
					hf005salprice:(hf005salprice/n).toFixed(1),
					hf011salnum:(hf011salnum/n).toFixed(1),
					hf011salprice:(hf011salprice/n).toFixed(1),
					
					ss001salnum:(ss001salnum/n).toFixed(1),
					ss001salprice:(ss001salprice/n).toFixed(1),
					ss002salnum:(ss002salnum/n).toFixed(1),
					ss002salprice:(ss002salprice/n).toFixed(1),
					
					
					pricetotal:(pricetotal/n).toFixed(1)
					}
	             });
		
			} 
		}
	});
}
//指定列求和
function compute_GXC(colName) {
     var rows = $('#dlPQJLMonth_datagrid').datagrid('getRows');
     var total = 0;
     for (var i = 0; i < rows.length; i++) {
    	 if(typeof(rows[i][colName]) != 'undefined'){
    		 total += parseFloat(rows[i][colName]);
    	 }
     }
     if(total > 0){
    	 return total.toFixed(1);
     }else{
    	 return 0;
     }
}
//查看门店报表详细信息
function lookInfo(ids){
	loadViewDialog('查看详细信息', '/shouba2.0/dlPQJLMonthTotalStatistic/lookInfo?organids=' + ids);
}
function searchFrom(){
	
	$('#dlPQJLMonth_datagrid').datagrid('load',sy.serializeObject($form));
}
function  dateFormatter(){
	$('#dlPQJLMonth_datebox').datebox({
	       //显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
	       onShowPanel: function () {
	          //触发click事件弹出月份层
	          span.trigger('click'); 
	          if (!tds)
	            //延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
	            setTimeout(function() { 
	                tds = p.find('div.calendar-menu-month-inner td');
	                tds.click(function(e) {
	                   //禁止冒泡执行easyui给月份绑定的事件
	                   e.stopPropagation(); 
	                   //得到年份
	                   var year = /\d{4}/.exec(span.html())[0] ,
	                   //月份
	                   //之前是这样的month = parseInt($(this).attr('abbr'), 10) + 1; 
	                   month = parseInt($(this).attr('abbr'), 10);  

				         //隐藏日期对象                     
				         $('#dlPQJLMonth_datebox').datebox('hidePanel').datebox('setValue', year + '-' + month); 
	                 });
	             }, 0);
	        },
	            //配置parser，返回选择的日期
	            parser: function (s) {
	                if (!s) return new Date();
	                var arr = s.split('-');
	                return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
	            },
	            //配置formatter，只返回年月 之前是这样的d.getFullYear() + '-' +(d.getMonth()); 
	            formatter: function (d) { 
	                var currentMonth = (d.getMonth()+1);
	                var currentMonthStr = currentMonth < 10 ? ('0' + currentMonth) : (currentMonth + '');
	                return d.getFullYear() + '-' + currentMonthStr; 
	            }
	        });

	        //日期选择对象
	        var p = $('#dlPQJLMonth_datebox').datebox('panel'), 
	        //日期选择对象中月份
	        tds = false, 
	        //显示月份层的触发控件
	        span = p.find('span.calendar-text'); 
	        var curr_time = new Date();

	        //设置前当月
	        $("#dlPQJLMonth_datebox").datebox("setValue", myformatter(curr_time));
    
 
}
//格式化日期
function myformatter(date) {
    //获取年份
    var y = date.getFullYear();
    //获取月份
    var m = date.getMonth() + 1;
    return y + '-' + m;
}


